<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-search"
          size="mini"
          @click="handleSearch"
          >搜索</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['project:jtm:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['project:jtm:edit']"
        >
          修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['project:jtm:remove']"
        >
          删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['project:jtm:export']"
        >
          导出
        </el-button>
      </el-col>
      <!--      <el-col :span="2.2" style="position: absolute;right: -10px;top:0" >
        <Page style="margin-right: 0px " :current="queryParams.pageNum" :page-size="queryParams.pageSize"  :total="total" @on-change="pageChange" simple></Page>
      </el-col>-->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      highlight-current-row
      border
      stripe
      :height="tableH"
      :row-style="{ padding: '0px' }"
      :cell-style="{ padding: '0px' }"
      :header-row-style="{ padding: '2px' }"
      :header-cell-style="{ 'text-align': 'center', padding: '2px' }"
      :data="jtmList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column label="#" type="index" width="55" align="center" />
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="机台编号"
        width="80"
        align="center"
        prop="jtmJtbh"
      />
      <el-table-column
        label="机台名称"
        width="80"
        align="center"
        prop="jtmJtmc"
      />
      <el-table-column
        label="机台吨位"
        width="80"
        align="center"
        prop="jtmJtdw"
      />
      <el-table-column
        label="购入日期"
        show-overflow-tooltip
        align="center"
        width="100"
        prop="jtmWgip"
      />
      <el-table-column
        label="组别代码"
        width="80"
        align="center"
        prop="jtmBbdm"
      >
        <template #default="scope">
          <dict-tag :options="itCarOptions" :value="scope.row.jtmBbdm" />
        </template>
      </el-table-column>
      <el-table-column label="正班工时" align="center" prop="jtmZbgs" />
      <el-table-column label="加班工时" align="center" prop="jtmJbgs" />
      <el-table-column label="总工时" align="center" prop="jtmZgs" />
      <el-table-column label="机台状态" align="center" prop="jtmZtbz">
        <template #default="scope">
          <dict-tag :options="bomZtbzOptions" :value="scope.row.jtmZtbz" />
        </template>
      </el-table-column>
      <el-table-column label="机台效率" align="center" prop="jtmJtxl" />
      <el-table-column label="有效工时" align="center" prop="jtmYxgs" />
      <el-table-column label="塑料材质" align="center" prop="jtmCzdm" />
      <el-table-column
        label="总功率(KW)"
        width="110"
        align="center"
        prop="jtmWldm"
      />
      <el-table-column
        label="模具最小宽度(mm)"
        width="140"
        align="center"
        prop="jtmMinkd"
      />
      <el-table-column
        label="模具最大宽度(mm)"
        width="140"
        align="center"
        prop="jtmMaxkd"
      />
      <el-table-column
        label="模具最小厚度(mm)"
        width="140"
        align="center"
        prop="jtmMinhd"
      />
      <el-table-column
        label="模具最大厚度(mm)"
        width="140"
        align="center"
        prop="jtmMaxhd"
      />
      <el-table-column label="有效标志" align="center" prop="jtmFlag">
        <template #default="scope">
          <el-switch
            v-model="scope.row.jtmFlag"
            active-value="0"
            inactive-value="1"
            @change="handleJtmFlag(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column
        label="描述"
        show-overflow-tooltip
        width="80"
        align="center"
        prop="remark"
      />
      <el-table-column
        label="IP地址"
        show-overflow-tooltip
        width="180"
        align="center"
        prop="jtmIpAddress"
      />
      <el-table-column label="创建人员" align="center" prop="createBy" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="150"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新人员" align="center" prop="updateBy" />
      <el-table-column
        label="更新时间"
        align="center"
        prop="updateTime"
        width="150"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.updateTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        fixed="right"
        width="120"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['project:jtm:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['project:jtm:remove']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改机台特性对话框 -->
    <el-dialog
      :title="title"
      v-model="open"
      :close-on-click-modal="false"
      draggable
      width="800px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-col :span="12">
          <el-form-item label="机台编号" prop="jtmJtbh">
            <el-input v-model="form.jtmJtbh" placeholder="请输入机台编号" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="机台名称" prop="jtmJtmc">
            <el-input v-model="form.jtmJtmc" placeholder="请输入机台名称" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="机台吨位" prop="jtmJtdw">
            <el-input v-model="form.jtmJtdw" placeholder="请输入机台吨位" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="购入日期" prop="jtmWgip">
            <el-input v-model="form.jtmWgip" placeholder="请输入购入日期" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="组别代码" prop="jtmBbdm">
            <el-select v-model="form.jtmBbdm" placeholder="请输入组别代码">
              <el-option
                v-for="dict in itCarOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="机台状态" prop="jtmZtbz">
            <el-select v-model="form.jtmZtbz" placeholder="请输入机台状态">
              <el-option
                v-for="dict in bomZtbzOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="正班工时" prop="jtmZbgs">
            <el-input v-model="form.jtmZbgs" @blur="changeZgs" />
          </el-form-item>
        </el-col>
        <el-col :span="2" class="unit-parent">
          <span class="unit">H</span>
        </el-col>
        <el-col :span="6">
          <el-form-item label="加班工时" prop="jtmJbgs">
            <el-input v-model="form.jtmJbgs" @blur="changeZgs" />
          </el-form-item>
        </el-col>
        <el-col :span="2" class="unit-parent">
          <span class="unit">H</span>
        </el-col>
        <el-col :span="6">
          <el-form-item label="总工时" prop="jtmZgs">
            <el-input v-model="form.jtmZgs" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="2" class="unit-parent">
          <span class="unit">H</span>
        </el-col>
        <el-col :span="6">
          <el-form-item label="有效工时" prop="jtmYxgs">
            <el-input
              v-model="form.jtmYxgs"
              @blur="changeXl"
              placeholder="请输入有效工时"
            />
          </el-form-item>
        </el-col>
        <el-col :span="2" class="unit-parent">
          <span class="unit">H</span>
        </el-col>
        <el-col :span="6">
          <el-form-item label="模具最小宽度" prop="jtmMinkd">
            <el-input
              v-model="form.jtmMinkd"
              placeholder="请输入模具最小宽度"
            />
          </el-form-item>
        </el-col>
        <el-col :span="2" class="unit-parent">
          <span class="unit">mm</span>
        </el-col>
        <el-col :span="6">
          <el-form-item label="模具最大宽度" prop="jtmMaxkd">
            <el-input
              v-model="form.jtmMaxkd"
              placeholder="请输入模具最大宽度"
            />
          </el-form-item>
        </el-col>
        <el-col :span="2" class="unit-parent">
          <span class="unit">mm</span>
        </el-col>
        <el-col :span="6">
          <el-form-item label="机台效率" prop="jtmJtxl">
            <el-input v-model="form.jtmJtxl" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="2" class="unit-parent">
          <span class="unit">%</span>
        </el-col>
        <el-col :span="6">
          <el-form-item label="模具最小厚度" prop="jtmMinhd">
            <el-input
              v-model="form.jtmMinhd"
              placeholder="请输入模具最小厚度"
            />
          </el-form-item>
        </el-col>
        <el-col :span="2" class="unit-parent">
          <span class="unit">mm</span>
        </el-col>
        <el-col :span="6">
          <el-form-item label="模具最大厚度" prop="jtmMaxhd">
            <el-input
              v-model="form.jtmMaxhd"
              placeholder="请输入模具最大厚度"
            />
          </el-form-item>
        </el-col>
        <el-col :span="2" class="unit-parent">
          <span class="unit">mm</span>
        </el-col>
        <el-col :span="12">
          <el-form-item v-if="form.menuType != 'F'">
            <span slot="label">
              <el-tooltip
                content="请输入*代替所有可生产的材质和产品"
                placement="top"
              >
                <i class="el-icon-question"></i>
              </el-tooltip>
              塑料材质
            </span>
            <el-input v-model="form.jtmCzdm" placeholder="请输入塑料材质" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item v-if="form.menuType != 'F'">
            <span slot="label">总功率(KW)</span>
            <el-input v-model="form.jtmWldm" placeholder="请输入总功率(KW)" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="有效标志">
            <el-radio-group v-model="form.jtmFlag">
              <el-radio
                v-for="dict in jtmFlagOptions"
                :key="dict.dictValue"
                :label="dict.dictValue"
                >{{ dict.dictLabel }}</el-radio
              >
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="IP地址" prop="jtmIpAddress">
            <el-input v-model="form.jtmIpAddress" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="描述" prop="remark">
            <el-input v-model="form.remark" placeholder="请输入描述" />
          </el-form-item>
        </el-col>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
    <!--    查询对话框-->
    <el-dialog
      :title="title"
      v-model="openSearch"
      :close-on-click-modal="false"
      draggable
      width="650px"
      append-to-body
    >
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
      >
        <el-col :span="12">
          <el-form-item label="机台编号" prop="jtmJtbh">
            <el-input
              v-model="queryParams.jtmJtbh"
              placeholder="请输入机台编号"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="机台名称" prop="jtmJtmc">
            <el-input
              v-model="queryParams.jtmJtmc"
              placeholder="请输入机台名称"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="机台吨位" prop="jtmJtdw">
            <el-input
              v-model="queryParams.jtmJtdw"
              placeholder="请输入机台吨位"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="购入日期" prop="jtmWgip">
            <el-input
              v-model="queryParams.jtmWgip"
              placeholder="请输入购入日期"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="组别代码" prop="jtmBbdm">
            <el-input
              v-model="queryParams.jtmBbdm"
              placeholder="请输入组别代码"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="有效标志" prop="jtmFlag">
            <el-select
              v-model="queryParams.jtmFlag"
              placeholder="请选择有效标志"
              clearable
              size="small"
            >
              <el-option
                v-for="dict in jtmFlagOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item
            style="margin-top: 25px; display: flex; justify-content: flex-end"
          >
            <el-button @click="cancel">取消</el-button>
            <el-button type="primary" @click="handleQuery">确认</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {
  listJtm,
  getJtm,
  delJtm,
  addJtm,
  updateJtm,
  exportJtm,
  changJtmFlag,
} from "@/api/project/jtm";

export default {
  name: "Jtm",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      tableH: document.body.clientHeight - 210,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      //机种信息
      jzmList: [],
      showSearch: true,
      // 总条数
      total: 0,
      // 机台特性表格数据
      itCarOptions: [],
      jtmList: [],
      // 状态标志字典
      bomZtbzOptions: [],
      // 弹出层标题
      title: "",
      openSearch: false,
      // 是否显示弹出层
      open: false,
      // 有效标志字典
      jtmFlagOptions: [],
      span: document.createElement("span"),
      spanPage: document.createElement("span"),
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 100,
        jtmJtbh: null,
        jtmJtmc: null,
        jtmJtdw: null,
        jtmWgip: null,
        jtmBbdm: null,
        jtmFlag: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        jtmJtbh: [
          { required: true, message: "机台编号不能为空", trigger: "blur" },
        ],
        jtmJtdw: [
          { required: true, message: "机台吨位不能为空", trigger: "blur" },
        ],
        jtmFlag: [
          { required: true, message: "有效标志不能为空", trigger: "blur" },
        ],
        createBy: [
          { required: true, message: "创建人员不能为空", trigger: "blur" },
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_normal_disable").then((response) => {
      this.jtmFlagOptions = response.data;
    });
    this.getDicts("machine_flag").then((response) => {
      this.bomZtbzOptions = response.data;
    });
    this.getDicts("Group_code").then((response) => {
      this.itCarOptions = response.data;
    });
  },
  methods: {
    handleSearch() {
      this.reset();
      this.title = "查询机台特性";
      this.openSearch = true;
    },
    /** 查询机台特性列表 */
    handleClick() {},
    getList() {
      this.loading = true;
      // const tableH = (document.body.clientHeight - 160) / 2
      listJtm(this.queryParams).then((response) => {
        // this.jtmList = response.rows
        this.jtmList = response.rows;

        this.total = response.total;

        this.loading = false;
      });
    },

    pageChange(page) {
      this.queryParams.pageNum = page;
      this.getList();
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openSearch = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        jtmId: null,
        jtmJtbh: null,
        jtmJtmc: null,
        jtmJtdw: null,
        jtmWgip: null,
        jtmBbdm: null,
        jtmZbgs: 0,
        jtmJbgs: 0,
        jtmZgs: 0,
        jtmZtbz: null,
        jtmJtxl: 0,
        jtmYxgs: 0,
        jtmCzdm: null,
        jtmWldm: null,
        jtmMinkd: 0,
        jtmMaxkd: 0,
        jtmMinhd: 0,
        jtmMaxhd: 0,
        jtmFlag: "0",
        remark: null,
        jtmIpAddress: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        menuType: "M",
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
      this.openSearch = false;
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.jtmId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加机台特性";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const jtmId = row.jtmId || this.ids;
      getJtm(jtmId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改机台特性";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.changeXl();
      this.changeZgs();
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.jtmId != null) {
            updateJtm(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addJtm(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const jtmIds = row.jtmId || this.ids;
      this.$confirm(
        '是否确认删除机台特性编号为"' + jtmIds + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delJtm(jtmIds);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    changeZgs() {
      this.form.jtmZgs =
        parseInt(this.form.jtmZbgs) + parseInt(this.form.jtmJbgs);
    },
    changeXl() {
      this.form.jtmJtxl = parseFloat(
        (this.form.jtmYxgs * 100) / this.form.jtmZgs
      ).toFixed(2);
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有机台特性数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.exportLoading = true;
          return exportJtm(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
          this.exportLoading = false;
        })
        .catch(() => {});
    },

    /** 修改状态 */

    handleJtmFlag(row) {
      let text = row.jtmFlag === "0" ? "启用" : "停用";

      this.$confirm('确认要"' + text + "吗?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return changJtmFlag(row.jtmId, row.jtmFlag);
        })
        .then(() => {
          this.getList();
          this.msgSuccess(text + "成功");
        })
        .catch(function () {
          row.jtmFlag = row.jtmFlag === "0" ? "1" : "0";
        });
    },
  },
};
</script>
<style lang="scss" scoped>
.unit-parent {
  height: 34px;
}

.unit {
  margin-left: 10px;
  height: 34px;
  line-height: 34px;
}
</style>
